<?php
class FavoriteAction extends CommonAction {
	protected function checkUser() {
		if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
			$this->assign('jumpUrl','Public/login');
			$this->error('没有登录');
		}
	}
	
	public function _before_index() {
		$this->checkUser();
		$name = $this->getActionName();
		$model = D($name);
		$map = array();
		$map['user_id'] = $_SESSION[C('USER_AUTH_KEY')];
		$rows   =   $model->where($map)->field('node_id')->select();
		$this->assign('favorite',$rows);
		
	
	}
	public function getNodeData(){
		$this->checkUser();
		if(isset($_SESSION['administrator'])){
			$model=D('Node');
			$rows=$model->where('level=2 and is_show=1')->field('id,title,name,nodeicon')->select();
		}
		else{
			$model=D('Role');
			$rows=$model->getAcceessMenu($_SESSION[C('USER_AUTH_KEY')]);
			//Log::write('调试的SQL语句：' . $model->getLastSql(), Log::SQL);
		}
		
		echo json_encode(array('Rows'=>$rows,'Total'=>count($rows)));
	}	
	
	public function saveFavorite()
	{
		$seqNoList =   $_POST['favoriteList'];
		if (!empty($seqNoList)) {
			//更新数据对象
			$name = $this->getActionName();
			$model = D($name);
			$model->where('user_id='.$_SESSION[C('USER_AUTH_KEY')])->delete() ;
			$col = explode(',', $seqNoList);
			//启动事务
			$model->startTrans();
			foreach ($col as $val) {
				$val = explode(':', $val);
				$model->user_id =$_SESSION[C('USER_AUTH_KEY')] ;
				$model->node_id = $val [0];
				//$model->sort = $val [1];
				$result = $model->add();
				if (!$result) {
					break;
				}
			}
			//提交事务
			$model->commit();
			if ($result !== false) {
				//采用普通方式跳转刷新页面'排序数据更新成功'
				$this->success('收藏快捷方式更新成功');
			} else {
				$this->error('错误：'.$model->getError());
			}
		}
	}
    public function sort()
    {
		$model = D('Node');;
        if(!empty($_GET['sortId'])) {
            $map = array();
           // $map['user_id'] = $_SESSION[C('USER_AUTH_KEY')];
            $map['id']   = array('in',$_GET['sortId']);
            $sortList   =   $model->where($map)->order('id asc')->select();
        }
        $this->assign("sortList",$sortList);
        $this->display();
        return ;
    }
    
    function saveSort() {
    	$seqNoList = $_POST ['seqNoList'];
    	if (!empty($seqNoList)) {
    		//更新数据对象
    		$name = $this->getActionName();
    		$model = D($name);
    		$col = explode(',', $seqNoList);
    		//启动事务
    		$model->startTrans();
    		foreach ($col as $val) {
    			$val = explode(':', $val);
    			$map = array();
    			$map['node_id'] = $val [0];
    			$map['user_id'] =$_SESSION[C('USER_AUTH_KEY')]; 
    			$result = $model->where($map)->setField('sort',$val [1]);
    			if (!$result) {
    				break;
    			}
    		}
    		//提交事务
    		$model->commit();
    		if ($result !== false) {
    			//采用普通方式跳转刷新页面'排序数据更新成功'
    			$this->success('<排序数据>更新成功');
    		} else {
    			$this->error($model->getError());
    		}
    	}
    }

}
?>